package com.olclass.web.service;

import com.olclass.web.dao.DataBizMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DataBizService {

    @Autowired
    DataBizMapper dataBizMapper;


    /**
     * 根据自定义sql语句查询数据
     * @param sql sql语句
     * @return
     */
    public List<Map> queryDataBySql(String sql){
        List<Map> maps = dataBizMapper.queryDataBySql(sql);
        return maps;
    }

    public void excuteSql(String sql){
        dataBizMapper.excuteSql(sql);
    }

    /**
     * 根据表名称查询所有字段
     * @param tableName 表名称
     * @return
     */
    public List<Map> queryAllData(String tableName,List<String> fieldNameList, String where){
        List<Map> list = dataBizMapper.queryAllData(tableName,fieldNameList,where);
        return list;
    }
/*

    */
/**
     * 查询数据
     * @param tableid 表id
     * @param fieldGroupType
     * @param pageSize
     * @param page
     * @param where
     * @return
     *//*

    public List<Map> queryPageData(String tableid, String fieldGroupType, int pageSize,int page, String where){
        List<String> fieldNameList = tbtskFieldsService.getTbtskFieldsNameListBySysCodeAndGroupCode("web",tableid,fieldGroupType);
        if(!fieldNameList.contains("f_id")){
            fieldNameList.add("f_id");
        }
        TbtskObjectinfo tbtskObjectinfo = tbtskObjectinfoService.getObjectbyID(tableid);
        List<Map> maps = dataBizMapper.queryPageData(tbtskObjectinfo.getfTablename(),fieldNameList, pageSize, (page-1)*pageSize, where);
        return maps;
    }

    public List<Map> queryPageData(String tableName, List<String> fieldNameList, int pageSize, int page, String where){
        List<Map> maps = dataBizMapper.queryPageData(tableName, fieldNameList, pageSize, (page-1)*pageSize, where);
        return maps;
    }

    public List<Map> queryPageData(String tableName, String tableid, String fieldGroupType, int pageSize,int page,String where){
        List<String> fieldNameList = tbtskFieldsService.getTbtskFieldsNameListBySysCodeAndGroupCode("web",tableid,fieldGroupType);
        List<String> fiedls = new ArrayList<>();
        for (String f : fieldNameList) {
            fiedls.add("d." + f);
        }
        fiedls.add("f_taskid");
        List<Map> maps = dataBizMapper.queryPageData(tableName, fiedls, pageSize, (page-1)*pageSize, where);
        return maps;
    }
*/

    /**
     * 根据ID查询数据对象
     * @param tableName 表名称
     * @param primaryFieldName 表主键字段名称
     * @param id 主键值
     * @return
     */
    public Map selectByID(String tableName,String primaryFieldName,String id){
        return dataBizMapper.selectByID(tableName,primaryFieldName,id);
    }

    /**
     * 根据ID查询数据对象
     * @param tableName 表名称
     * @param fieldList 字段列表
     * @param primaryFieldName 表主键字段名称
     * @param id 主键值
     * @return
     */
    public Map selectFieldByID(String tableName, List<String> fieldList, String primaryFieldName,String id){
        return dataBizMapper.selectFieldByID(tableName, fieldList, primaryFieldName, id);
    }

    /**
     * 查询数据总数量
     * @param tableName 表名称
     * @param where 条件
     * @return 数量
     */
    public int getCount(String tableName,String where){
        int count = dataBizMapper.getCount(tableName, where);
        return count;
    }

    /**
     * 插入数据
     *
     * @param tableName 表名称
     * @param map       数据Map对象
     * @return
     */
    public int insertData(String tableName, Map<String, Object> map, String geofield) {

        int insert = 0;
        insert = dataBizMapper.insert(tableName, map, geofield);

        return insert;
    }

    /**
     * 批量插入数据
     *
     * @param tableName
     * @param datalist
     * @param geoField
     * @return
     */
    public int insertDatasBatch(String tableName, List<Map<String, Object>> datalist, String geoField) {
        return dataBizMapper.insertBatch(tableName, datalist, geoField);
    }

    /**
     * 更新数据
     *
     * @param tableName 表名称
     * @param map       更新值的Map对象
     * @param where     条件
     * @return
     */
    public int updateData(String tableName, Map<String, Object> map, String where) {
        int insert = dataBizMapper.update(tableName, map, where);
        return insert;
    }

    /**
     * 删除数据
     * @param tableName 表名称
     * @param primaryField 表主键字段名称
     * @param id 主键值
     * @return
     */
    public int delete(String tableName, String primaryField, String id){
        int delete = dataBizMapper.delete(tableName, primaryField, id);
        return delete;
    }

    /**
     * 根据条件删除数据
     * @param tableName 表名称
     * @param where 删除条件
     * @return
     */
    public int deleteByCond(String tableName,String where){
        int i = dataBizMapper.deleteByCond(tableName, where);
        return i;
    }

    /**
     * 根据sql执行更新
     *
     * @param tableName 表名称
     * @param sql       更新的语句
     * @param where     更新条件
     * @return
     */
    public int updateBySql(String tableName, String sql, String where) {
        return dataBizMapper.updateBySql(tableName, sql, where);
    }

    public boolean isExistTableName(String tableName){
        Assert.hasLength(tableName);
        String sql = "select count(*) as c from pg_class where relname = '"+tableName+"'";
        return ((Long) dataBizMapper.queryDataBySql(sql).get(0).get("c")) > 0;
    }

}
